package com.banmao.algorithm.No1To100.No20;

import java.util.Stack;

public class Solution1 {

    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();

        char[] chars = s.toCharArray();
        for (char aChar : chars) {
            if(aChar == ')' || aChar == ']' || aChar == '}') {
                if(stack.isEmpty()) {
                    return false;
                }
                Character pop = stack.pop();
                Character temp = null;
                switch (aChar) {
                    case ')':
                        temp = '(';
                        break;
                    case ']':
                        temp = '[';
                        break;
                    case '}':
                        temp = '{';
                        break;
                }
                if(temp != pop) {
                    return false;
                }

            } else {
                stack.push(aChar);
            }
        }
        return stack.isEmpty();

    }

}
